package bancosys.tec.job.dispatcher;

import jmine.tec.batch.BatchController;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import bancosys.tec.job.Job;
import bancosys.tec.job.JobDispatcher;
import bancosys.tec.job.JobDispatchingException;
import bancosys.tec.job.JobExecutionException;
import bancosys.tec.job.JobMessages;

/**
 * @author Rafael Volpato
 */
public class SyncJobDispatcher implements JobDispatcher {

    private static Log log = LogFactory.getLog(SyncJobDispatcher.class);

    private BatchController batchController;

    /**
     * Executa o job.
     * 
     * @param job o job a ser executado.
     * @throws JobDispatchingException caso não consguida executar o job.
     */
    public void dispatch(Job job) throws JobDispatchingException {
        try {
            this.batchController.clearBatchExecutionContext();
            job.execute(this.batchController.getBatchExecutionContext());
        } catch (JobExecutionException e) {
            log.error("Job[" + job.getId() + "]: execution failed.", e);
            throw new JobDispatchingException(JobMessages.GENERIC_EXECUTION_ERROR.create(), e);
        }
    }

    /**
     * @return the batchController
     */
    public BatchController getBatchController() {
        return this.batchController;
    }

    /**
     * @param batchController the batchController to set
     */
    public void setBatchController(BatchController batchController) {
        this.batchController = batchController;
    }
}
